package edu.uba.fcen.estimacion.word.selection;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import edu.uba.fcen.estimacion.word.selection.constants.Constants;

public class WriterResult {

	private String fileName;
	private BufferedWriter writer;
	private String path;

	public WriterResult(String fileName, String path) {
		this.fileName = fileName;
		this.path = path;
		Writer out;
		try {
			out = new OutputStreamWriter(new FileOutputStream(
					this.path + this.fileName),
					Constants.ENCODING);
			
			writer = new BufferedWriter(out);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
	}
	
	public WriterResult(String fileName) {
		this(fileName, "");
	}

	public void writeWithEnter(String text) {
		try {
			this.writer.append(text);
			this.writer.append(System.getProperty("line.separator"));
			this.writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void write(String text) {
		try {
			this.writer.append(text);
			this.writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void close() {
		try {
			this.writer.flush();
			this.writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
